/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
 */

package com.ewerp.mud.core.session.sockets;

import java.io.IOException;
import java.net.Socket;

import com.ewerp.mud.commands.ICommand;
import com.ewerp.mud.commands.ICommandEngine;
import com.ewerp.mud.core.exceptions.FailedToPushCommandException;
import com.ewerp.mud.core.session.parsers.TerminalLineParser;
import com.ewerp.mud.entities.admin.Admin;


public class EwerpAdminSocketSession extends EwerpBaseSocketSession {
    protected enum SessionState {CONNECTED, AUTHENTICATED};
    
    protected SessionState currentState = SessionState.CONNECTED;
    
    private Admin admin = new Admin(this);
    
    public EwerpAdminSocketSession(Socket socket) {
        super(socket);
    }

    @Override
    protected void execute() throws Exception {
        switch(currentState) {
            case AUTHENTICATED: {
                String input = read();
                if(!input.equalsIgnoreCase("shutdown")) {
                    processCommand(input);
                } else {
                    shutdown();
                }
            } break;
            case CONNECTED: {
                write("Welcome to the mud. You are logged into admin mode:");
                newLine();
                currentState = SessionState.AUTHENTICATED;
            } break;
        }
    }
    
    protected void processCommand(String input) throws IllegalArgumentException, FailedToPushCommandException, IOException {
        ICommand command = null;
        
        command = TerminalLineParser.getInstance().processPacket(admin, input);
        
        if(command != null) {
            command.setPluginManager(getPluginManager());

            ((ICommandEngine)getPluginManager().getPlugin(ICommandEngine.class)).pushCommand(command);
        }
    }
    
}